﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Sonce.Controls
{
    public partial class Menu : ItemsControl
    {
        #region Members
        private Orientation orientation = Orientation.Horizontal;
        #endregion

        #region Constructor
        public Menu()
            : base()
        {
            this.DefaultStyleKey = typeof(Menu);
            this.Orientation = Orientation.Horizontal;
            this.VerticalAlignment = VerticalAlignment.Top;
            this.HorizontalAlignment = HorizontalAlignment.Left;
        }
        #endregion

        #region Properties
        public bool IsNavigating
        {
            get;
            set;
        }

        public Orientation Orientation
        {
            get
            {
                return this.orientation;
            }
            set
            {
                this.orientation = value;
                if (ItemsContainer != null)
                {
                    switch (this.orientation)
                    {
                        case Orientation.Vertical:
                            this.ItemsPanel = (ItemsPanelTemplate)ItemsContainer.Resources["ItemsPanelVertical"];
                            break;

                        case Orientation.Horizontal:
                            this.ItemsPanel = (ItemsPanelTemplate)ItemsContainer.Resources["ItemsPanelHorizontal"];
                            break;
                    }
                }
            }
        }
        #endregion

        #region Methods
        public void HidePopup()
        {
            foreach (UIElement element in this.Items)
            {
                if (element is MenuItem)
                {
                    MenuItem menuItem = element as MenuItem;
                    if (menuItem.IsExpanded)
                    {
                        menuItem.IsExpanded = false;
                    }
                }
            }
        }
        #endregion

        #region Template
        protected FrameworkElement RootElement;
        protected StackPanel ItemsContainer;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            RootElement = (FrameworkElement)GetTemplateChild("RootElement");
            ItemsContainer = (StackPanel)GetTemplateChild("ItemsContainer");
            this.Orientation = this.orientation;
        }
        #endregion

        #region Bubble events
        public event EventHandler<MenuEventArgs> ItemClick;
        internal virtual void OnItemClick(MenuEventArgs e)
        {
            if (ItemClick != null)
            {
                ItemClick(this, e);
            }
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            if (this.IsNavigating)
            {
                this.IsNavigating = false;
                this.HidePopup();
            }
            base.OnMouseLeftButtonUp(e);
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            if (!this.IsNavigating)
            {
                HidePopup();
            }
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
        }
        #endregion
    }

    public class MenuEventArgs : EventArgs
    {
        public object OriginalSource;

        public MenuEventArgs()
        {
        }

        public MenuEventArgs(object source)
        {
            this.OriginalSource = source;
        }
    }
}
